package atomgit_hook

import (
	"time"

	"atomgit.com/openlinksaas-org/proto-gen-go.git/events/events_atomgit"
)

type Issue struct {
	Url           string `json:"url"`
	RepositoryUrl string `json:"repository_url"`
	LabelsUrl     string `json:"labels_url"`
	CommentsUrl   string `json:"comments_url"`
	EventsUrl     string `json:"events_url"`
	HtmlUrl       string `json:"html_url"`
	Id            string `json:"id"`
	Number        string `json:"number"`
	Title         string `json:"title"`
	User          User   `json:"user"`
	State         string `json:"state"`
	Locked        bool   `json:"locked"`
	CreatedAt     string `json:"created_at"`
	UpdatedAt     string `json:"updated_at"`
	Body          string `json:"body"`
}

func (item *Issue) ToProtoBuf() *events_atomgit.Issue {
	createTime := int64(0)
	t, err := time.Parse(time.RFC3339, item.CreatedAt)
	if err == nil {
		createTime = t.UnixNano() / 1e6
	}
	updateTime := int64(0)
	t, err = time.Parse(time.RFC3339, item.UpdatedAt)
	if err == nil {
		updateTime = t.UnixNano() / 1e6
	}
	return &events_atomgit.Issue{
		Url:           item.Url,
		RepositoryUrl: item.RepositoryUrl,
		LabelsUrl:     item.LabelsUrl,
		CommentsUrl:   item.CommentsUrl,
		EventsUrl:     item.EventsUrl,
		HtmlUrl:       item.HtmlUrl,
		Id:            item.Id,
		Number:        item.Number,
		Title:         item.Title,
		User:          item.User.ToProtoBuf(),
		State:         item.State,
		Locked:        item.Locked,
		CreatedAt:     createTime,
		UpdatedAt:     updateTime,
		Body:          item.Body,
	}
}

type IssueHook struct {
	Action     string     `json:"action"`
	Issue      Issue      `json:"issue"`
	Repository Repository `json:"repository"`
	Sender     User       `json:"sender"`
}

func (item *IssueHook) ToProtoBuf() *events_atomgit.IssueEvent {
	return &events_atomgit.IssueEvent{
		Action:     item.Action,
		Issue:      item.Issue.ToProtoBuf(),
		Repository: item.Repository.ToProtoBuf(),
		Sender:     item.Sender.ToProtoBuf(),
	}
}
